import Vue from 'vue'

//判断是否登录（所有端）
export const judgeLogin = (callback) => {
	var _this = Vue.prototype;
	_this.$u.get('/system/user/getTmplId',{
		appId : _this.website.appId
	}).then(res => {
		_this.$store.commit('setWxLogin', res.isLogin);
		if("1" == res.isTip){
			var time = uni.getStorageSync("time");
			if(time){
				var now = new Date().getTime();
				var minte = now/1000/60;
				var cha = minte - time;
				if(cha < 1){
					//test(callback);
					return;
				}
			}
			
			var myId = uni.getStorageSync('userId');
			if(myId){
				df(callback, res.tmplIds);
			}else{
				//test(callback);
			}

		}else{
			//test(callback);
		}
		
		
	});
	
	test(callback);
	
}

function df(callback, tmplIds) {
	var _this = Vue.prototype;
	
	var tmplId = tmplIds[0];
	wx.getSetting({
	  withSubscriptions: true,
	  success(res) {
		console.log(res)
		if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
		  if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplId]) {
			let item = res.subscriptionsSetting.itemSettings[tmplId]
			if (item == "reject") {
			  uni.showModal({
			  	title: '温馨提示',
			  	content: '请打开通知管理里面的消息提醒',
			  	showCancel: false,
			  	success: function(res) {
			  		uni.openSetting({
					  success(res) {
						console.log(res.authSetting)
					  },
					  fail(err) {
						console.error(err);
					  }
					});
			  	}
			  });
			} else if (item == "accept") {
			  uni.requestSubscribeMessage({
			  	//数组，最多放三个订阅模板的ID
			  	tmplIds: tmplIds,
			  	success: function(res) {
			  		var code = res[tmplIds[0]];
			  		if('reject' == code){
			  			_this.$u.toast('请点击允许订阅消息');
			  		}else{
						var time = new Date().getTime();
						uni.setStorageSync("time", time/1000/60);
						
						_this.$u.get('/system/user/getMyTip').then(res => {
						});
						
						//test(callback);
						return;
			  		}
			  	},
			  	fail: function(res) {
			  		console.log(res)
			  	},
			  	complete: (res) => {
			  		
			  	}
			  });
			} else if (item == "ban") {
			  uni.showModal({
			  	title: '温馨提示',
			  	content: '请在设置打开订阅消息开关',
			  	showCancel: false,
			  	success: function(res) {
			  		
			  	}
			  });
			}
		  }else{
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: tmplIds,
				success: function(res) {
					var code = res[tmplIds[0]];
					if('reject' == code){
						_this.$u.toast('请点击允许订阅消息');
					}else{
						var time = new Date().getTime();
						uni.setStorageSync("time", time/1000/60);
						
						_this.$u.get('/system/user/getMyTip').then(res => {
						});
						
						//test(callback);
						return;
					}
				},
				fail: function(res) {
					console.log(res)
				},
				complete: (res) => {
					
				}
			});
		  }
		} else {
		  uni.showModal({
		  	title: '温馨提示',
		  	content: '请在设置打开通知管理开关',
		  	showCancel: false,
		  	success: function(res) {
		  		uni.openSetting({
				  success(res) {
					console.log(res.authSetting)
				  },
				  fail(err) {
					console.error(err);
				  }
				});
		  	}
		  });
		}
	  }
	});
}

// 放行
function test(callback) {
	var _this = Vue.prototype;
	_this.$u.get('/system/user/getMyInfo').then(res => {
		_this.user = res.user;
		_this.$store.commit('setUser', _this.user);
		_this.$store.commit('setCashRate', _this.user.cashRate);
		_this.$store.commit('setNoticeNum', _this.user.giveNewNum+_this.user.commentNewNum);
		if(_this.user.isWelcome == 0){
			_this.$u.route({
				url: 'packgeMine/pages/me/welcome/welcome',
			});
		}else{
			callback(res);
		}
	});
}

//判断会员次数是否不足（所有端）
export const judgeVipNum = (callback) => {
	var _this = Vue.prototype;
	_this.$u.get('/system/user/getMyNum').then(res => {
		callback(res.user)
	});
}

//判断余额是否不足（所有端）
export const judgeCoin = (coinNum, callback) => {
	var _this = Vue.prototype;
	_this.$u.get('/pay/userCoin/getAmount').then(res => {
		if(coinNum > res.data){
			_this.$u.toast('余额不足，请充值');
			_this.$store.commit('setCoin', true);
			_this.$store.commit('setAmount', res.data);
		}
		callback(res.data)
	});
}

export const judgeAuth = (callback) => {
	var _this = Vue.prototype;
	_this.$u.get('/system/user/getMyAsk').then(res => {
		callback(res)
	});
}